﻿using System;
using System.Text;
using System.Data;

using LTJ.Common.Generic;
using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;

namespace LTJ.Data.DataProvider
{
    /// <summary>
    /// 规则库操作
    /// </summary>
    public class Rules
    {
        /// <summary>
        /// 增设规则
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="oid">企业ID</param>
        /// <param name="rtype">规则类型</param>
        /// <param name="_20GP">20GP价格</param>
        /// <param name="_40GP">40GP价格</param>
        /// <param name="_40HQ">40HQ价格</param>
        /// <param name="rid">航线ID</param>
        /// <param name="port">港口</param>
        public static int CreateRules(int uid, int oid, int rtype, string _20GP, string _40GP, string _40HQ, int rid, string port, string seacode)
        {
            return DatabaseProvider.GetInstance().CreateRules(uid, oid, rtype, _20GP, _40GP, _40HQ, rid, port, seacode);
        }

        /// <summary>
        /// 是否存在规则信息
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="oid">企业ID</param>
        /// <param name="rtype">规则类型</param>
        /// <param name="rid">航线ID</param>
        /// <param name="port">港口</param>
        public static bool ExistRule(int uid, int oid, int rtype, int rid, string port, string seacode)
        {
            return DatabaseProvider.GetInstance().ExistRule(uid, oid, rtype, rid, port, seacode);
        }

        /// <summary>
        /// 获取规则数量
        /// </summary>
        public static int GetRulesCount(string condition)
        {
            return DatabaseProvider.GetInstance().GetRulesCount(condition);
        }

        /// <summary>
        /// 规则基本信息分页操作（DataTable对象)
        /// </summary>
        public static DataTable GetRulesByPage(int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            return DatabaseProvider.GetInstance().GetRulesByPage(pageindex, pagesize, condition, ordercolumn, ordertype);
        }

        /// <summary>
        /// 批量删除操作
        /// </summary>
        public static void DeleteRulesByIds(string ids)
        {
            DatabaseProvider.GetInstance().DeleteRulesByIds(ids);
        }

        /// <summary>
        /// 获取规则实体
        /// </summary>
        public static RuleInfo GetRuleInfoByID(int id)
        {
            return LoadSingleRule(DatabaseProvider.GetInstance().GetRuleInfoByID(id));
        }

        /// <summary>
        /// 修改规则价格设置
        /// </summary>
        public static void UpdateRulePrice(int id, string price1, string price2, string price3)
        {
            DatabaseProvider.GetInstance().UpdateRulePrice(id, price1, price2, price3);
        }

        /// <summary>
        /// 其他公司航线价格分页操作
        /// </summary>
        public static DataTable GetRoutePriceRuleByPage(int type, int uid, int cid, int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            return DatabaseProvider.GetInstance().GetRoutePriceRuleByPage(type, uid, cid, pageindex, pagesize, condition, ordercolumn, ordertype);
        }

        /// <summary>
        /// 其他公司线路价格规则批量调整
        /// </summary>
        /// <param name="ids">线路ID集合</param>
        public static void UpdateRules(string ids, int uid, string price1, string price2, string price3)
        {
            DatabaseProvider.GetInstance().UpdateRules(ids, uid, price1, price2, price3);
        }

        /// <summary>
        /// 其他公司线路价格规则批量调整（用户批量）
        /// </summary>
        public static void UpdateRules(string ids, string uids, string price1, string price2, string price3)
        {
            DatabaseProvider.GetInstance().UpdateRules(ids, uids, price1, price2, price3);
        }

        #region Help
        private static RuleInfo LoadSingleRule(IDataReader reader)
        {
            RuleInfo _info = null;

            if (reader.Read())
            {
                _info = new RuleInfo();
                _info.id = TypeConverter.ObjectToInt(reader["id"]);
                _info.uid = TypeConverter.ObjectToInt(reader["uid"]);
                _info.oid = TypeConverter.ObjectToInt(reader["oid"]);
                _info.rtype = TypeConverter.ObjectToInt(reader["rtype"]);
                _info._20GP = reader["20GP"].ToString();
                _info._40GP = reader["40GP"].ToString();
                _info._40HQ = reader["40HQ"].ToString();
                _info.rid = TypeConverter.ObjectToInt(reader["rid"]);
                _info.port = reader["port"].ToString();
                _info.seaname = reader["seaname"].ToString();
                _info.seaename = reader["seaename"].ToString();
                _info.boatname = reader["boatname"].ToString();
                _info.boatename = reader["boatename"].ToString();
                _info.portname = reader["portname"].ToString();
                _info.portename = reader["portename"].ToString();
                _info.cname = reader["cname"].ToString();
                _info.lname = reader["lname"].ToString();
                _info.boatcode = reader["bcode"].ToString();
                _info.seacode = reader["seacode"].ToString();
                _info.seaname1 = reader["seaname1"].ToString();
                _info.seaename1 = reader["seaename1"].ToString();
            }

            reader.Close();
            return _info;
        }
        #endregion
    }
}
